<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span="23">
        <el-form ref="form" :model="form" label-width="150px">
          <el-form-item
            :rules="[{ required: true, message: '请输入'}]"
            label="名称"
            prop="CategoryName"
          >
            <el-input
              v-model="form.CategoryName"
              placeholder="中文"
            />
          </el-form-item>
          <el-form-item
            :rules="[{ required: true, message: '请输入'}]"
            label="编号"
            prop="CategoryCode"
          >
            <el-input
              v-model="form.CategoryCode"
              placeholder="小写英文_号分割"
            >
              <el-button slot="append" icon="el-icon-s-order" @click="formatCode" />
            </el-input>
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.ADD" label="类型" prop="CategoryType" :rules="[{ required: true, message: '请输入'}]">
            <el-select v-model="form.CategoryType">
              <el-option v-for="item in enums.dictTypeEnum" :key="item.value" :label="item.text" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="Remarks">
            <el-input v-model="form.Remarks" type="textarea" />
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.ADD">
            <el-button type="primary" @click="add">
              保存
            </el-button>
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.DETAIL">
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.EDIT">
            <el-button type="primary" @click="edit">
              保存
            </el-button>
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import operateEnum from '@/enums/operateEnum'
import apolloClient from '@/utils/graphql'
import gql from 'graphql-tag'
import dictTypeEnum from '@/enums/sys/dictTypeEnum'

export default {
  name: 'DictCategoryInfo',
  props: {
    // id
    id: {
      type: String,
      required: false
    },
    // 操作类型, 可用  edit, add
    operateType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单数据
      form: {
        CategoryName: '',
        CategoryCode: '',
        Remarks: '',
        CategoryType: null,
        DictionaryCategoryId: this.id
      },
      enums: {
        operateEnum,
        dictTypeEnum
      }
    }
  },
  mounted() {
    if (this.id) {
      this.getDetail()
    }
  },
  methods: {
    formatCode() {
      if (this.form.CategoryCode) {
        this.form.CategoryCode = this.form.CategoryCode.replaceAll('.', '_').replaceAll('-', '_').toLowerCase()
      }
    },
    getDetail() {
      apolloClient.query({
        fetchPolicy: 'network-only',
        query: gql`query($unionId: String!){
                  dictCategoryDetail(dictCategoryId:$unionId){
                    DictionaryCategoryId
                    CategoryName
                    CategoryCode
                    Remarks
                  }
                }`,
        variables: { unionId: this.id }
      }).then(response => {
        if (response.data) {
          var responseData = response.data.dictCategoryDetail
          responseData.__typename = undefined
          this.form = responseData
          if (this.form.dictCategoryDetail) {
            this.form.dictCategoryDetail.__typename = undefined
          }
        } else {
          this.$notify.error(response.errors[0].message)
        }
      }).catch((err) => {
        this.$notify.error(err.message)
      })
    },
    // 保存数据
    add() {
      // 验证表单数据
      this.$refs.form.validate((valid) => {
        if (valid) {
          var postData = {}
          Object.assign(postData, this.form)
          apolloClient.mutate({
            mutation: gql`mutation ($req: DictCategoryAddReq!) {
                            dictCategoryAdd(Req: $req)
                        }`,
            variables: { req: postData }
          }).then(response => {
            if (response.data) {
              this.$notify.success('保存成功')
              this.$emit('addComplete')
            } else {
              this.$notify.error(response.errors[0].message)
            }
          }).catch((err) => {
            this.$notify.error(err.message)
          })
        }
      })
    },
    // 保存数据
    edit() {
      // 验证表单数据
      this.$refs.form.validate((valid) => {
        if (valid) {
          apolloClient.mutate({
            mutation: gql`mutation($req:DictCategoryEditReq!){
                          dictCategoryEdit(Req:$req)
                        }`,
            variables: { req: this.form }
          }).then(response => {
            if (response.data) {
              this.$notify.success('保存成功')
              this.$emit('editComplete')
            } else {
              this.$notify.error(response.errors[0].message)
            }
          }).catch((err) => {
            this.$notify.error(err.message)
          })
        }
      })
    },
    close() {
      this.$emit('doClose')
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
